import model from './model'
import MixinsList from '@screen/mixins/initList'
import API1 from '../../API/select/index'
import API2 from '../../API/list.js'
const API = { ...API1, ...API2 }

export default {
  provide() {
    return {
      DICT: this.DICT,
    }
  },
  /**
   * 混入（列表）
   */
  mixins: [MixinsList],
  data() {
    return {
      model: model(),
      API: API,
      currentView: 'actual',
      DICT: {},
    }
  },
  methods: {
    async getList(model) {
      const data = {
        // 搜索
        ...this.model.searchData,
        startTime: this.startTime,
        endTime: this.endTime,
        // 页码
        page: this.model.pagination.page,
        // 每页记录数
        pageSize: this.model.pagination.pageSize,
      }
      const [num, arr] = await this.API.getList(data)
      this.list = arr
      this.model.pagination.total = num
    },
    changeView(view) {
      if (view === 'actual') {
        this.API.getList = API2.getList
      } else {
        this.API.getList = API2.getHistoryList
      }
      this.toSearch()
    },

    /**
     * @description:获取下拉列表
     */
    getDictList() {
      API1.forEach((api) => {
        api.getList().then((res) => {
          const [prop, list] = res
          this.DICT[prop] = list
        })
      })
    },
    /**
     * @description:处理告警
     * @param {*} row 当前行的数据
     * @param {*} type 告警类型
     * @return {*}
     */
    async deal(row, type) {
      row.dealType = type
      try {
        await this.API.update(row)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        error && this.$message.error(error.message)
      }
    },
  },
  created() {
    this.getDictList()
  },
}
